//package com.scau402.system.service.impl;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import com.scau402.common.annotation.DataScope;
//import com.scau402.common.constant.WarehouseConstants;
//import com.scau402.common.core.domain.AjaxResult;
//import com.scau402.common.core.domain.entity.SysUser;
//import com.scau402.common.utils.DateUtils;
//import com.scau402.common.utils.SecurityUtils;
//import com.scau402.common.utils.StringUtils;
//import com.scau402.common.utils.code.WarehouseStorageCodeUtils;
//import com.scau402.system.domain.SysTenant;
//import com.scau402.system.domain.WmsInboundOrder;
//import com.scau402.system.domain.WmsWarehouse;
//import com.scau402.system.service.ISysTenantService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import com.scau402.system1.mapper.WmsWarehouseStorageMapper;
//import com.scau402.system.domain.WmsWarehouseStorage;
//import com.scau402.system.service.IWmsWarehouseStorageService;
//
///**
// * 仓库仓位Service业务层处理
// *
// * @author ruoyi
// * @date 2023-06-26
// */
//@Service
//public class WmsWarehouseStorageServiceImpl implements IWmsWarehouseStorageService
//{
//    @Autowired
//    private WmsWarehouseStorageMapper wmsWarehouseStorageMapper;
//
//    @Autowired
//    private ISysTenantService tenantService;
//
//
//
//    /**
//     * 查询仓库仓位
//     *
//     * @param id 仓库仓位主键
//     * @return 仓库仓位
//     */
//    @Override
//    public WmsWarehouseStorage selectWmsWarehouseStorageById(Long id)
//    {
//        return wmsWarehouseStorageMapper.selectWmsWarehouseStorageById(id);
//    }
//
//    /**
//     * 查询仓库仓位列表
//     *
//     * @param wmsWarehouseStorage 仓库仓位
//     * @return 仓库仓位
//     */
//    @Override
//    @DataScope(tenantAlias = "t", userAlias = "u")
//    public List<WmsWarehouseStorage> selectWmsWarehouseStorageList(WmsWarehouseStorage wmsWarehouseStorage)
//    {
//        wmsWarehouseStorage.setTenantId(SecurityUtils.getLoginUser().getTenantId());
//        return wmsWarehouseStorageMapper.selectWmsWarehouseStorageList(wmsWarehouseStorage);
//    }
//
//    /**
//     * 新增仓库仓位
//     *
//     * @param wmsWarehouseStorage 仓库仓位
//     * @return 结果
//     */
//    @Override
//    public int insertWmsWarehouseStorage(WmsWarehouseStorage wmsWarehouseStorage)
//    {
//        wmsWarehouseStorage.setCreateTime(DateUtils.getNowDate());
//        wmsWarehouseStorage.setCreateBy(SecurityUtils.getLoginUser().getUsername());
//        wmsWarehouseStorage.setTenantId(SecurityUtils.getLoginUser().getTenantId());
//        return wmsWarehouseStorageMapper.insertWmsWarehouseStorage(wmsWarehouseStorage);
//    }
//
//    /**
//     * 修改仓库仓位
//     *
//     * @param wmsWarehouseStorage 仓库仓位
//     * @return 结果
//     */
//    @Override
//    public int updateWmsWarehouseStorage(WmsWarehouseStorage wmsWarehouseStorage)
//    {
//        wmsWarehouseStorage.setUpdateTime(DateUtils.getNowDate());
//        wmsWarehouseStorage.setTenantId(SecurityUtils.getLoginUser().getTenantId());
//        return wmsWarehouseStorageMapper.updateWmsWarehouseStorage(wmsWarehouseStorage);
//    }
//
//    /**
//     * 批量删除仓库仓位
//     *
//     * @param ids 需要删除的仓库仓位主键
//     * @return 结果
//     */
//    @Override
//    public AjaxResult deleteWmsWarehouseStorageByIds(Long[] ids)
//    {
//        AjaxResult ajax = AjaxResult.success();
//        List<String> success = new ArrayList<>();
//        List<String> fail = new ArrayList<>();
//        for(Long id : ids) {
//            WmsWarehouseStorage storage = selectWmsWarehouseStorageById(id);
//            if(storage.getUsedCap().equals(WarehouseConstants.WAREHOUSE_USED_CAP_EMPTY)) {
//                success.add("仓位‘"+storage.getStorageName()+"’删除");
//                wmsWarehouseStorageMapper.deleteWmsWarehouseStorageById(id);
//            } else {
//                fail.add("仓位‘"+storage.getStorageName()+"’当前存在货品，无法删除");
//            }
//        }
//        ajax.put("success", success);
//        ajax.put("fail", fail);
//        return ajax;
//    }
//
//    /**
//     * 删除仓库仓位信息
//     *
//     * @param id 仓库仓位主键
//     * @return 结果
//     */
//    @Override
//    public int deleteWmsWarehouseStorageById(Long id)
//    {
//        return wmsWarehouseStorageMapper.deleteWmsWarehouseStorageById(id);
//    }
//
//    @Override
//    public String getMaxCode(SysUser user) {
//        Long tenantId = user.getTenantId();
//        return wmsWarehouseStorageMapper.getMaxCode(tenantId);
//    }
//
//    @Override
//    public String getWarehouseStorageCode(String loginUserTenantCode) throws InterruptedException {
//        WarehouseStorageCodeUtils warehouseStorageCodeUtils = new WarehouseStorageCodeUtils();
//        return warehouseStorageCodeUtils.getNewCode(loginUserTenantCode);
//    }
//
//    @Override
//    public String getNewCode(SysUser user) throws InterruptedException {
//        // 获取数据库中最大的编码
//        String maxCode = getMaxCode(user);
//
//        // 返回maxCode为NULL，及当前数据库没有数据，从0开始
//        if(StringUtils.isNull(maxCode)) {
//            maxCode = "0";
//        }
//
//        // 获取当前用户所属租户编码
//        Long loginUserTenantId = SecurityUtils.getLoginUser().getTenantId();
//
//        SysTenant tenant = tenantService.selectSysTenantByTenantId(loginUserTenantId);
//
//        String loginUserTenantCode = tenant.getTenantCode();
//
//        // 设置redis的当前编码
//        WarehouseStorageCodeUtils warehouseStorageCodeUtils = new WarehouseStorageCodeUtils();
//        warehouseStorageCodeUtils.setCurrentCodeToMax(maxCode, loginUserTenantCode);
//
//        // 返回仓库编码
//        return getWarehouseStorageCode(loginUserTenantCode);
//    }
//
//    @Override
//    public void setDefaultStorage(WmsWarehouse warehouse) throws InterruptedException {
//        WmsWarehouseStorage storage = new WmsWarehouseStorage();
//        // 设置仓位编码
//        storage.setStorageCode(getNewCode(SecurityUtils.getLoginUser().getUser()));
//        // 设置为不限容量
//        storage.setMaxCap(WarehouseConstants.WAREHOUSE_MAXCAP_INFINITE);
//        // 设置默认仓位名称
//        storage.setStorageName(warehouse.getWarehouseName() + "-" + WarehouseConstants.WAREHOUSE_STORAGE_DEFAULT_NAME);
//        // 设置所属仓库主键ID
//        storage.setParentWarehouse(warehouse.getId());
//        // 设置所属父仓位为空（-1l)
//        storage.setParentStorage(WarehouseConstants.DEFAULT_PARENT_STORAGE);
//        // 设置仓位初始窗台为 0启用
//        storage.setStatus(WarehouseConstants.WAREHOUSE_STATUS_ENABLE);
//        // 写入
//        insertWmsWarehouseStorage(storage);
//    }
//
//    @Override
//    public void deleteWmsWarehouseStorageByParentWarehouseId(Long id) {
//        wmsWarehouseStorageMapper.deleteWmsWarehouseStorageParentWarehouseId(id);
//    }
//
//    @Override
//    public boolean checkFull(WmsInboundOrder wmsInboundOrder) {
//        WmsWarehouseStorage storage = selectWmsWarehouseStorageById(wmsInboundOrder.getDesStorageId());
//        // 检查仓位容量是否已满
//        if(!storage.getMaxCap().equals(WarehouseConstants.WAREHOUSE_MAXCAP_INFINITE) && ((storage.getUsedCap()+wmsInboundOrder.getTotalQuantity()) > storage.getMaxCap())) {
//            // 容量已满
//            return false;
//        }
//        return true;
//    }
//
//    /**
//     * 更新仓位数量
//     * @param wmsInboundOrder
//     * @return
//     */
//    @Override
//    public int updateQuantity(WmsInboundOrder wmsInboundOrder) {
//        WmsWarehouseStorage storage = selectWmsWarehouseStorageById(wmsInboundOrder.getDesStorageId());
//        storage.setUsedCap(storage.getUsedCap() + wmsInboundOrder.getTotalQuantity());
//        return(updateWmsWarehouseStorage(storage));
//    }
//
//    @Override
//    public boolean checkCodeUnique(WmsWarehouseStorage wmsWarehouseStorage) {
//        if(wmsWarehouseStorageMapper.chekcCodeUnique(wmsWarehouseStorage) > 0) {
//            return false;
//        }
//        return true;
//    }
//}
